<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Session extends CI_Controller {

	public function index() { }

	//CHECK SESSION
	public function check_session() {
		if( $this->session->userdata('admin') != null) {
			// echo $this->session->userdata('username');
			echo 'admin';
			// redirect(base_url().'index.php/user#/home');
		}
		else echo '901nosession';
	}

	//GET LOGIN
	public function getLogin(){
		$user = $this->input->post('user');
		$username = $user['username'];
		$password = $user['password'];

		$this->load->model('Model_user');	
		$ck = null;
		$ck = $this->Model_user->getUser($username, $password);
		$arr = array();
		foreach(iterator_to_array($ck) as $point) {
			array_push($arr, $point);
		}
		if(isset($arr[0])) //if login success
        {
            $catchSession = array(
                'admin' => $username,
                'is_admin_in' => true
            );
            session_start();
            $this->session->set_userdata($catchSession);
            echo 'Đăng nhập thành công';
        }  
        else 
        {
            echo 'Username hoặc Password không đúng!' ;
        }

	}

	//GET LOGOUT
	public function getLogout(){
        if($this->is_login()){
           $this->session->unset_userdata(array('admin'=>'', 'is_admin_in'=>false));
    	}
	}

}

?>